// +----------------------------------------------------------------------
// | JavaWeb_EleVue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2021 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <javaweb520@gmail.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.admin.controller;

import com.javaweb.admin.entity.RateLimiter;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.enums.LogType;
import com.javaweb.common.common.BaseController;
import com.javaweb.admin.entity.KdlSaleOutQc;
import com.javaweb.admin.query.KdlSaleOutQcQuery;
import com.javaweb.admin.service.IKdlSaleOutQcService;
import com.javaweb.common.annotation.Log;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.entity.User;
import com.javaweb.system.utils.ShiroUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import static com.javaweb.admin.constant.KdlCallConstant.DOCTYPE.DOCTYPE_Z04KDL;


/**
 * <p>
 * 销售出库质检 前端控制器
 * </p>
 *
 * @author 鲲鹏
 * @since 2024-02-21
 */
@RestController
@RequestMapping("/kdlsaleoutqc")
public class KdlSaleOutQcController extends BaseController {

    @Autowired
    private IKdlSaleOutQcService kdlSaleOutQcService;

    private RateLimiter limiter = new RateLimiter();




    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    //@RequiresPermissions("sys:kdlsaleoutqc:index")
    @GetMapping("/index")
    public JsonResult index(KdlSaleOutQcQuery query) {
        User user = ShiroUtils.getUserInfo();
        String username = user.getUsername();
        return kdlSaleOutQcService.getSaleOutQCList(query);
    }

    @GetMapping("/detail")
    public JsonResult detail(KdlSaleOutQcQuery query) {
        return JsonResult.success(kdlSaleOutQcService.getSaleOutDetailList(query.getVendorso()));
    }
    /**
     * 添加记录
     *
     * @param entity 实体对象
     * @return
     */
    @Log(title = "销售出库质检", logType = LogType.INSERT)
    @RequiresPermissions("sys:kdlsaleoutqc:add")
    @PostMapping("/add")
    public JsonResult add(@RequestBody KdlSaleOutQc entity) {
        return kdlSaleOutQcService.edit(entity);
    }

    /**
     * 获取详情
     *
     * @param kdlsaleoutqcId 记录ID
     * @return
     */
    @GetMapping("/info/{kdlsaleoutqcId}")
    public JsonResult info(@PathVariable("kdlsaleoutqcId") Integer kdlsaleoutqcId) {
        return kdlSaleOutQcService.info(kdlsaleoutqcId);
    }

    /**
     * 更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Log(title = "销售出库质检", logType = LogType.UPDATE)
    @RequiresPermissions("sys:kdlsaleoutqc:edit")
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody KdlSaleOutQc entity) {
        return kdlSaleOutQcService.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param kdlsaleoutqcIds 记录ID
     * @return
     */
    @Log(title = "销售出库质检", logType = LogType.DELETE)
    @RequiresPermissions("sys:kdlsaleoutqc:delete")
    @DeleteMapping("/delete/{kdlsaleoutqcIds}")
    public JsonResult delete(@PathVariable("kdlsaleoutqcIds") Integer[] kdlsaleoutqcIds) {
        return kdlSaleOutQcService.deleteByIds(kdlsaleoutqcIds);
    }


    /**
     *
     * @param entity
     * @return
     */
    @Log(title = "销售出库质检回传", logType = LogType.INSERT)
    @PostMapping("/syncsaleoutqc")
    public JsonResult syncSaleOutQC(@RequestBody KdlSaleOutQcQuery entity) {
        if(StringUtils.isEmpty(entity.getPrincipalid())) {
            entity.setPrincipalid(CommonConfig.kdlCompanyId);
        }
        if(StringUtils.isEmpty(entity.getDoctype())) {
            entity.setDoctype(DOCTYPE_Z04KDL.value());
        }
        if(StringUtils.isEmpty(entity.getStarttime())) {
            //获取最新同步数据的时间
            entity.setStarttime(kdlSaleOutQcService.GetMaxSyncTime());

        }
        if(StringUtils.isEmpty(entity.getEndtime())) {
            entity.setEndtime(DateUtils.getTime());
        }
        updateCancelSaleOutQC(entity);
        return kdlSaleOutQcService.syncSaleOutQC(entity);
    }

    public void updateCancelSaleOutQC(KdlSaleOutQcQuery entity) {
        try {
            String maxSyncTime = entity.getStarttime();
            String pur3hours = DateFormatUtils.format(DateUtils.addHours(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, maxSyncTime), -3), DateUtils.YYYY_MM_DD_HH_MM_SS);
            String pur1day = DateFormatUtils.format(DateUtils.addDays(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, maxSyncTime), -1), DateUtils.YYYY_MM_DD_HH_MM_SS);
            String pur2day = DateFormatUtils.format(DateUtils.addDays(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, maxSyncTime), -2), DateUtils.YYYY_MM_DD_HH_MM_SS);
            String pur3day = DateFormatUtils.format(DateUtils.addDays(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, maxSyncTime), -3), DateUtils.YYYY_MM_DD_HH_MM_SS);
            Runnable task_3hours = () -> kdlSaleOutQcService.updateCancelSaleOutQC(entity, pur3hours, maxSyncTime);
            Runnable task_1day = () -> kdlSaleOutQcService.updateCancelSaleOutQC(entity, pur1day, pur3hours);
            Runnable task_2day = () -> kdlSaleOutQcService.updateCancelSaleOutQC(entity, pur2day, pur1day);
            Runnable task_3day = () -> kdlSaleOutQcService.updateCancelSaleOutQC(entity, pur3day, pur2day);
            limiter.tryInvoke("A",task_3hours,limiter.INTERVAL30min);
            limiter.tryInvoke("B",task_1day,limiter.INTERVAL4HOURS);
            limiter.tryInvoke("C",task_2day,limiter.INTERVAL1DAY);
            limiter.tryInvoke("C",task_3day,limiter.INTERVAL1DAY);
        } catch (BeansException e) {

        }
    }
}